package com.cloud.lan.pub.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 全局 异常通知工具 (支持钉钉robot、微信方糖、企微robot)
 * @author ErKang
 * @date Created in 2022/6/1 16:26
 * @title TalkUtil.java
 * Department: Product development
 */
@Slf4j
@Component
public class TalkUtil {

    private static String env = "dev";

    /**
     * 钉钉异常通知
     * @author ErKang
     * @return void
     * 2020/8/11 18:05
     */
    public static void dingRobot(String text, String context) {
        Map<String, Object> mes = new HashMap<>();
        mes.put("title", text);
        mes.put("text", "#### **【系统" + text + "「" + env + "」】** [通知到位](https://www.qekang.com/) \n >" + context.replaceAll("#", ""));
        HashMap<String, Object> params = MapUtil.of("msgtype", "markdown");
        params.put("markdown", mes);

        Map<String, Object> at = new HashMap<>();
        at.put("atMoboles", text);
        params.put("at", at);

        System.out.println(Json.toString(params));
        String result = HttpUtil.post("https://oapi.dingtalk" +
                ".com/robot/send?access_token=7d04cc36174ea319cda40641fab7433ec5a0f7288d764b36edc6c92827a79b64", Json.toString(params));
        log.info(result);
    }

    /**
     * 微信公众号异常通知
     * @author ErKang
     * @return void
     * 2020/8/11 18:05
     */
    public static void weChatRobot(String context) {
        Map<String, Object> mes = new HashMap<>();
        mes.put("text", "兰嘉丝汀异常监控「" + env + "」");
        mes.put("desp", "#### **【系统异常监控「" + env + "」】**  [通知到位](https://www.qekang.com/) \n ``` " + context + " ```");
        HttpUtil.post("https://sc.ftqq.com/SCU109066Te7928d8458d9b4c645dd4e2d68e5644b5f32527e6596d.send", mes);
    }

    /**
     * 企业微信异常通知
     * @author ErKang
     * @return void
     * 2020/8/11 18:05
     */
    public static void weComRobot(String context) {
        //待接入
    }

    public static void main(String[] args) {
//        DingRobot("心跳测试", "心跳测试");
//        Map<String, Object> mes = new HashMap<>();
//        mes.put("text", "异常监控");
//        mes.put("desp", "#### **【系统异常监控】**  [已通知到位](https://www.qekang.com/)``` dddddd ```");
//        HttpUtil.post("https://sc.ftqq.com/SCU109066Te7928d8458d9b4c645dd4e2d68e5644b5f32527e6596d.send", mes);

//        Map<String, Object> mes = new HashMap<>();
//        mes.put("title", "异常监控");
//        mes.put("text", "#### **【系统异常监控】** [已通知到位](https://www.qekang.com/) \n>```text com/robot/send?access_tokencontext ```\n ");
//        HashMap<String, Object> params = MapUtil.of("msgtype", "markdown");
//        params.put("markdown", mes);
//        System.out.println(Json.toString(params));
//        String result = HttpUtil.post("https://oapi.dingtalk.com/robot/send?access_token=3833566c715aa7f1de175c711800d45f2a87f639c356a1192cbfcee9ee8bc41b",
//        Json.toString(params));
//        log.info(result);

    }

    @Value("${spring.profiles.active}")
    public void setEnv(String env) {
        TalkUtil.env = env;
    }
}
